package com.apexedu.portal.service.impl;

import com.apexedu.biz.Const;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.portal.action.PortalRoalAction;
import com.apexedu.portal.entity.TYhzd;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by janabo on 2014-9-16.
 */
@Service("portalRoalService")
public class PortalRoalServiceImpl extends AbstractBaseServiceSupport {

    @SuppressWarnings("unchecked")
    @Override
    public List<TYhzd> list(Object myaction, Pager pager) throws Exception {
        List<TYhzd> list = DBUtil.queryAllBeanList("select * from t_yhzd t", TYhzd.class);
        return list;
    }

    @Override
    public void saveOrUpdate(Object myaction) throws Exception {
        PortalRoalAction action = (PortalRoalAction) myaction;
        TYhzd yhzd = action.getYhzd();
        if (yhzd != null) {
            if (StringUtil.isNotBlank(yhzd.getWid())) {
                baseDao.updateNotNull(yhzd);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_YHZD, Const.LOG_ACTION_UPDATE, IdLoginAction.getIpAddr(action.getRequest()));
            } else {
                yhzd.setWid(SeqFactory.getNewSequenceAlone());
                baseDao.save(yhzd);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_YHZD, Const.LOG_ACTION_SAVE, IdLoginAction.getIpAddr(action.getRequest()));

            }
        }
    }

    @Override
    public void load(Object myaction) throws Exception {
        PortalRoalAction action = (PortalRoalAction) myaction;
        String wid = action.getWid();
        TYhzd yhzh = getBaseDao().findById(TYhzd.class, wid);
        action.setYhzd(yhzh);
    }

    @Override
    public boolean remove(Object myaction) throws Exception {
        PortalRoalAction action = (PortalRoalAction) myaction;
        String ids = action.getWid();
        boolean deleteSuccess = getBaseDao().deleteAll("TYhzd", "wid", "=", ids);
        BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_YHZD, Const.LOG_ACTION_DEL, IdLoginAction.getIpAddr(action.getRequest()));
        return deleteSuccess;
    }
}
